package org.example.controller;


import jakarta.mail.MessagingException;
import org.example.service.MyAsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

@RestController
public class AsyncController {

    @Autowired
    MyAsyncService myAsyncService;

    @RequestMapping("/sendSMS")
    public String sendMsg()
    {
        //主线程的开始时间
        System.out.println("主流程开始工作");
        Long startTime = System.currentTimeMillis();
        //子线程
        myAsyncService.SendSMS();
        Long endTime = System.currentTimeMillis();
        System.out.println("主流程耗时： "+(endTime-startTime));
        return "success";
    }


    @GetMapping("/statistics")
    //主线程要等两个子线程的运行结果主线程才能结束
    public String statistics() throws Exception {
        Long startTime = System.currentTimeMillis();
        Future<Integer> futureA = myAsyncService.processA();
        Future<Integer> futureB = myAsyncService.processB();
        int total = futureA.get() + futureB.get();
        System.out.println("异步任务数据统计汇总结果： "+total);
        Long endTime = System.currentTimeMillis();
        System.out.println("主流程耗时： "+(endTime-startTime));
        return "success";}


    @RequestMapping("sendMail")
    public String  sendMail()
    {
        myAsyncService.SendSimpleMailService();
        return "发送成功";
    }

    /**
     * String from,
     *                                     String to,
     *                                     String cc,
     *                                     String subject,
     *                                     String content,
     *                                     String jpgPath,
     *                                     String filePath
     * @return
     */
    //1235415456454
//    @RequestMapping("sendMimeMail")
    @Scheduled(cron = "0 28 12 * * ?")
    public void  sendMimeMail() throws MessagingException {
        myAsyncService.sendMimeMailService("864369912@qq.com",
                "502914391@qq.com",
                "864369912@QQ.COM",
                "我的测试邮件",
                "贾博斐",
                "E:\\pic.jpg",
                "E:\\软件测试\\灯泡评估.docx");
    }

}
